#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initUIs();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initUIs()
{
    ui->tb_open->setText(QChar(0xf115));
    ui->tb_char->setText(QChar(0xf00c));
    ui->tb_find->setText(QChar(0xf002));

    ui->gb_find->setVisible(false);

    textBrowser = new CodeEditor;
    QFont font3;
    font3.setPointSize(16);
    textBrowser->setFont(font3);

    ui->verticalLayout_2->addWidget(textBrowser);
    ui->verticalLayout_2->setStretch(1, 1);
}

//
void MainWindow::initCheckStates()
{
    ui->cb_1->setChecked(true);
    ui->cb_2->setChecked(true);
    ui->cb_abc->setChecked(true);
    ui->cb_time->setChecked(true);
    ui->cb_space->setChecked(true);
}


void MainWindow::on_tb_find_clicked()
{
    auto findStr = ui->le_find->text();

}


void MainWindow::resetOri()
{
    auto ret = origin.readAll();
//    QTextDocument doc;
//    doc.setPlainText("sdsdsdsdsd");
//    doc.addResource(QTextDocument::ImageResource)
//    ui->textBrowser->setDocument(&doc);
    textBrowser->appendPlainText(ret);
    HighLight * h = new HighLight(textBrowser->document());//传一个QTextDocument
}

void MainWindow::replaceChar(QChar c)
{
    auto s = textBrowser->document()->toPlainText();
    LastStr = s;
    s.remove(c);
    textBrowser->setPlainText(s);
}

void MainWindow::replaceIndex(int n)
{
    auto s = textBrowser->document()->toPlainText();
    LastStr = s;
}



void MainWindow::on_actionApply_triggered()
{

}

void MainWindow::on_tb_open_clicked()
{
    auto file = QFileDialog::getOpenFileName();
    if(file.isEmpty()){

    }else{
        initCheckStates();
        ui->le_Flann->setText(file);
    }
    QFile *logFile = new QFile(file);
    logFile->open(QIODevice::ReadOnly);
    origin.setDevice(logFile);
    resetOri();
}

void MainWindow::on_cb_time_clicked(bool checked)
{
    QString pattern("\\[\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{1,2}:\\d{1,2} \\d{6}\\]");
    if(!checked)
        textBrowser->setPlainText(removeStr(pattern));
    else{
        textBrowser->setPlainText(LastStr);
    }
}

void MainWindow::on_actionFont_triggered()
{
    bool ok;
    QFont font = QFontDialog::getFont(
                  &ok, QFont("Helvetica [Cronyx]", 10), this);
    if (ok) {
      textBrowser->setFont(font);
    } else {
    }
}

void MainWindow::on_cb_space_clicked(bool checked)
{
    if(!checked)
        replaceChar(' ');
    else{
        textBrowser->setPlainText(LastStr);
    }
}



void MainWindow::on_cb_abc_clicked(bool checked)
{

}

QString MainWindow::SplitLine(int n)
{
    QTextDocument* doc=textBrowser->document () ; 

    LastStr = doc->toPlainText();
    QString newStr;
    int cnt=doc->blockCount () ;
    for (int i=0; i<cnt;i++)
    {
        QTextBlock textLine=doc->findBlockByNumber (i) ;     
         if(i % 2 == n){
            QString str=textLine.text();
            newStr+=str;
            newStr += "\n";
        }else{

        }
    }

    return  newStr;

}

QString MainWindow::removeStr(QString pattern)
{
    QRegularExpression re(pattern);
    QTextDocument* doc=textBrowser->document () ; //文本对象

    LastStr = doc->toPlainText();
    QString newStr;
    int cnt=doc->blockCount () ;
    for (int i=0; i<cnt;i++)
    {
        QTextBlock textLine=doc->findBlockByNumber (i) ;
        QString str=textLine.text();
        QRegularExpressionMatch match=re.match(str);
        str.remove(re);
        newStr+=str;
        newStr += "\n";
    }
    return newStr;
}


void MainWindow::on_cb_1_clicked(bool checked)
{
    if(!checked){
        textBrowser->setPlainText(SplitLine(1));
    }else{
        textBrowser->setPlainText(LastStr);
    }
}

void MainWindow::on_cb_2_clicked(bool checked)
{
    if(!checked){
        textBrowser->setPlainText(SplitLine(0));
    }else{
        textBrowser->setPlainText(LastStr);
    }
}

void MainWindow::on_actionSaveAs_triggered()
{
    OpenSeleteFile();
}



bool MainWindow::OpenSeleteFile()
{
   QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
                               "temp.txt",
                               tr("log (*.txt *.log *.*)"));

    if(fileName.isEmpty())
    {
        // QMessageBox::information(this,QString::fromUtf8("提示"),QString::fromUtf8("选择文件失败，无文件"),"OK");
        return false;
    }else{
        QFile fileSave(fileName);
        //创建数据�?        // QDataStream dsOut(&fileSave);
        if(fileSave.open(QIODevice::WriteOnly)){
            auto s = textBrowser->document()->toPlainText();
            fileSave.write(s.toUtf8());
        }

    }
    return true;
}

